package edu.gdin.ecommerce.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

	private static final DateFormat[] ACCEPT_DATE_FORMATS = {  
        new SimpleDateFormat("dd/MM/yyyy"),  
        new SimpleDateFormat("yyyy-MM-dd"),  
        new SimpleDateFormat("yyyy/MM/dd"),
        new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"),
	}; //支持转换的日期格式   
	
	public static Date now() {
		return new Date();
	}
	
	public static String dateFormat(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}
	
	
	public static String dateFormat(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
	
	public static Date parseDate(String dateStr) {
		for (DateFormat format : ACCEPT_DATE_FORMATS) {   
            try {   
                return format.parse(dateStr);//遍历日期支持格式，进行转换   
            } catch(Exception e) {   
                continue;   
            }   
        }   
		return null;
		
	}
	
}
